﻿
using System;
using Castle.MicroKernel.Registration;
using Castle.MicroKernel;
using System.Collections;
namespace Inovout
{
    public partial class ComponentRegistry
    {
        public static void Initialize(IApplication application)
        {
            componentRegistry = new ComponentRegistry(AppDomain.CurrentDomain,application);
    
        }
        //暂支持一个AppDomain，未来可拓展到多个AppDomain
        private static ComponentRegistry componentRegistry;



        public static T Resolve<T>(string serviceName, params object[] args)
        {
            Arguments arguments = new Arguments();
            foreach (var arg in args)
            {
                arguments.Insert(arg.GetType(), arg);
            }
            return componentRegistry.Container.Resolve<T>(serviceName, arguments);
        }
        public static T Resolve<T>()
        {
            return componentRegistry.Container.Resolve<T>();
        }
        public static T Resolve<T>(string serviceName)
        {
            return componentRegistry.Container.Resolve<T>(serviceName);
        }
        public static T Resolve<T>(Type serviceType)
        {
            return (T)componentRegistry.Container.Resolve(serviceType);
        }
        public static bool TryResolve<T>(string serviceName, out T serviceInstance)
        {
            if (HasComponent(serviceName))
            {
                serviceInstance = Resolve<T>(serviceName);
                return true;
            }
            else
            {
                serviceInstance = default(T);
                return false;
            }
        }
        public static bool HasComponent(Type modelType)
        {
            return componentRegistry.Container.Kernel.HasComponent(modelType);
        }
        public static bool HasComponent(string serviceName)
        {
            return componentRegistry.Container.Kernel.HasComponent(serviceName);
        }

        public static T[] ResolveAll<T>()
        {
            return componentRegistry.Container.ResolveAll<T>();
        }

        public static object Resolve(Type serviceType)
        {
            return componentRegistry.Container.Resolve(serviceType);
        }

        public static void AddComponent(string serviceName, Type serviceType)
        {
            componentRegistry.Container.Register(Component.For(serviceType).Named(serviceName));
        }
        public static void AddComponent(string serviceName, Type serviceType, Type serviceInstanceType)
        {
            componentRegistry.Container.Register(Component.For(serviceType).ImplementedBy(serviceInstanceType).Named(serviceName));
        }
        public static void AddComponent(Type serviceType, Type serviceInstanceType)
        {
            componentRegistry.Container.Register(Component.For(serviceType).ImplementedBy(serviceInstanceType));
        }
        public static void AddComponent<TService>(TService componentObject) where TService : class
        {
            componentRegistry.Container.Kernel.Register(Component.For<TService>().Instance(componentObject).IsDefault());
        }
        public static void AddComponent<TService>(string serviceName, TService serviceInstance) where TService : class
        {
            componentRegistry.Container.Kernel.Register(Component.For<TService>().Named(serviceName).Instance(serviceInstance));
        }
        public static object Resolve(Type serviceType, IDictionary arguments)
        {
            return componentRegistry.Container.Resolve(serviceType, arguments);
        }

        public static void Release(object serviceInstance)
        {
            componentRegistry.Container.Release(serviceInstance);
        }
    }
}
